/**
 * ReportGear(2011)
 */
package com.reportgear.report.view.grid.editor.impl;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import com.reportgear.core.swing.layout.LayoutHelper;
import com.reportgear.core.swing.pane.base.EditorPane;
import com.reportgear.core.swing.pane.base.JBaseDialog;
import com.reportgear.report.core.api.idname.IdName;
import com.reportgear.report.model.cell.Cell;
import com.reportgear.report.model.cell.value.salary.SalaryDetailObject;
import com.reportgear.report.model.cell.value.salary.SalaryDocObject;
import com.reportgear.report.view.grid.Grid;
import com.reportgear.report.view.grid.editor.AbstractCellEditor;
import com.sot.core.util.StringUtils;

/**
 * 薪酬档案对象编辑器
 * 
 * @version 1.0 2011-6-4
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class SalaryDetailObjectEditor extends AbstractCellEditor {
	private SalaryDetailObject value;

	@Override
	public Component getCellEditorComponent(Grid grid, Cell cell) {
		SalaryDetailObjectEditPane editorPane = new SalaryDetailObjectEditPane();

		editorPane.populate((SalaryDetailObject) cell.getValue());

		if (editorPane.showDialog(grid) == JBaseDialog.RESULT_OK) {
			this.value = editorPane.update();
		}

		return null;
	}

	@Override
	public Object getCellEditorValue() {
		return this.value;
	}

	private static class SalaryDetailObjectEditPane extends EditorPane<SalaryDetailObject> {
		/**
		 * UID
		 */
		private static final long serialVersionUID = -5356419132881129425L;
		private SalaryDetailObject value;
		private JComboBox cbxField;

		private SalaryDetailObjectEditPane() {
			this.setBorder(BorderFactory.createEtchedBorder());
			this.setLayout(new GridBagLayout());
			this.cbxField = new JComboBox(SalaryDocObject.getAllObjectInfo().toArray());

			this.add(new JLabel("档案信息", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 0, 1, 1));
			this.add(cbxField, LayoutHelper.newGridBagConstraints(1, 0, 1, 1));
			cbxField.setPreferredSize(new Dimension(300, 24));
		}

		@SuppressWarnings("unchecked")
		@Override
		public void populate(SalaryDetailObject value) {
			this.value = value;

			String id = value.getField();
			if (StringUtils.isEmpty(id)) {
				return;
			}

			for (int i = 0, size = cbxField.getItemCount(); i < size; i++) {
				IdName<String> item = (IdName<String>) cbxField.getItemAt(i);

				if (id.equals(item.getId())) {
					cbxField.setSelectedIndex(i);
					return;
				}
			}
		}

		@Override
		public int showDialog(Component owner) {
			return this.showDialog(SwingUtilities.getWindowAncestor(owner), "组织机构(成本中心)", 460, 140);
		}

		@SuppressWarnings("unchecked")
		@Override
		public SalaryDetailObject update() {
			IdName<String> item = (IdName<String>) cbxField.getSelectedItem();

			if (item != null) {
				this.value.setField(item.getId());
				this.value.setName(item.getName());
			}

			return this.value;
		}
	}
}
